//
//  CSGXDatePicker.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/12/28.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXDatePicker.h"

@interface CSGXDatePicker () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, weak) UILabel *lblHeader;
@property (nonatomic, weak) UIButton *btnOK;
@property (nonatomic, weak) UIPickerView *vpk;
@property (nonatomic, weak) UIView *vCover;


///数据源
@property (nonatomic, assign) NSInteger componentCount;

@property (nonatomic, strong) NSArray<NSString *> *years;
@property (nonatomic, strong) NSArray<NSString *> *months;
@property (nonatomic, strong) NSArray<NSString *> *days;

@property (nonatomic, strong) NSArray<NSString *> *timeHourList;
@property (nonatomic, strong) NSArray<NSString *> *timeMinList;


@property (nonatomic, copy) NSString *currentYearStr;
@property (nonatomic, copy) NSString *currentMonthStr;
@property (nonatomic, copy) NSString *currentDayStr;

@property (nonatomic, strong) NSString *currentHour;
@property (nonatomic, strong) NSString *currentMin;

@property (nonatomic, strong) NSMutableArray<UIView *> *linesM;


@end

@implementation CSGXDatePicker


#pragma mark - Actions
- (void)btnOKClick:(UIButton *)sender {
    [self pickerDisappeare];
    
    NSString *time = @"";
    if (self.pickerType == PickerTypeDate) {
        //
        time = [NSString stringWithFormat:@"%@-%@-%@", self.currentYearStr, [self get2Str:[self.currentMonthStr subStringFrontOfLastIndex:1]], [self get2Str:self.currentDayStr]];
    } else {
        //
        time = [NSString stringWithFormat:@"%@:%@", self.currentHour, self.currentMin];
        
    }
    if ([self.delegate respondsToSelector:@selector(gxDataPicker:time:)]) {
        [self.delegate gxDataPicker:self time:time];
    }
}

- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
    [self pickerDisappeare];
}


#pragma mark - methods

- (void)pickerDisappeare {
    [self.vCover removeFromSuperview];
    [self removeFromSuperview];
}

- (void)initForDate {
    self.componentCount = 3;
    [self initLinesWithCount:6];
    
    self.years = [self getYearList];
    self.months = [self getMonthList];
    self.days = [self getDaysWithCount:31];
    [self.vpk reloadAllComponents];
    
    [self.vpk selectRow:self.years.count - 2 inComponent:0 animated:NO];
    [self.vpk selectRow:0 inComponent:1 animated:NO];
    [self.vpk selectRow:0 inComponent:2 animated:NO];
    
    [self pickerView:self.vpk didSelectRow:self.years.count - 2 inComponent:0];
    [self pickerView:self.vpk didSelectRow:0 inComponent:1];
    [self pickerView:self.vpk didSelectRow:0 inComponent:2];
}

- (void)initForTime {
    self.componentCount = 2;
    [self initLinesWithCount:4];

    self.timeHourList = [self getHourList];
    self.timeMinList = [self getMinList];
    
    [self.vpk reloadAllComponents];
    
    [self.vpk selectRow:self.timeHourList.count - 1 inComponent:0 animated:NO];
    [self.vpk selectRow:self.timeMinList.count - 1 inComponent:1 animated:NO];
    
    [self pickerView:self.vpk didSelectRow:self.timeHourList.count - 1 inComponent:0];
    [self pickerView:self.vpk didSelectRow:self.timeMinList.count - 1 inComponent:1];
}


- (NSArray<NSString *> *)getYearList {
    NSString *currentYear = [CSGXDateFormatterManager sharedManager].currentYear;
    NSInteger currYearN = [currentYear integerValue];
    NSMutableArray<NSString *> *marr = [NSMutableArray<NSString *> array];
    for (NSInteger i = 9; i >= 0; i--) {
        [marr addObject:@(currYearN + 1 - i).description];
    }
    return marr.copy;
}

- (NSArray<NSString *> *)getMonthList {
    return @[@"1月", @"2月", @"3月", @"4月", @"5月", @"6月", @"7月", @"8月", @"9月", @"10月", @"11月", @"12月"];
}

- (NSArray<NSString *> *)getDayListWithYear:(NSString *)year month:(NSString *)month {
    //
    NSInteger count = 0;
    BOOL isLeap = [self isLeapYear:year];
    if (isLeap) {
        count = 29;
    }else {
        count = 28;
    }
    NSArray<NSString *> *arr = [NSArray<NSString *> array];
    NSInteger monthN = [[month subStringFrontOfLastIndex:1] integerValue];
    switch (monthN) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            arr = [self getDaysWithCount:31];
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            arr = [self getDaysWithCount:30];
            break;
        case 2:
            arr = [self getDaysWithCount:count];
            break;
            
        default:
            break;
    }
    return arr;
}

- (BOOL)isLeapYear:(NSString *)year {
    NSInteger yearN = [year integerValue];
    if ((yearN % 4 == 0 && yearN % 100 != 0) || yearN % 400 == 0) {
        return YES;
    }else {
        return NO;
    }
}

- (NSArray<NSString *> *)getDaysWithCount:(NSInteger)count {

    NSMutableArray<NSString *> *marr = [NSMutableArray<NSString *> array];
    for (int i = 1; i <= count; i++) {
        [marr addObject:@(i).description];
    }
    return marr.copy;
}

- (NSString *)get2Str:(NSString *)str1 {
    NSString *str = @"";
    if (str1.length == 1) {
        str = [NSString stringWithFormat:@"0%@", str1];
    }else {
        str = str1;
    }
    return str;
}

//
- (NSArray<NSString *> *)getHourList {
    NSMutableArray<NSString *> *marr = [NSMutableArray<NSString *> array];
    for (int i = 0; i < 24; i++) {
        [marr addObject:[self get2Str:@(i).description]];
    }
    return marr.copy;
}

- (NSArray<NSString *> *)getMinList {
    NSMutableArray<NSString *> *marr = [NSMutableArray<NSString *> array];
    for (int i = 0; i < 60; i++) {
        [marr addObject:[self get2Str:@(i).description]];
    }
    return marr.copy;
}

#pragma mark - delegates
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.componentCount;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSInteger count = 0;
    if (self.pickerType == PickerTypeDate) {
        //
        if (component == 0) {
            count = self.years.count;
        } else if (component == 1) {
            count = self.months.count;
        } else if (component == 2) {
            count = self.days.count;
        }
    } else if (self.pickerType == PickerTypeTime) {
        //
        if (component == 0) {
            count = self.timeHourList.count;
        } else if (component == 1) {
            count = self.timeMinList.count;
        }
    }
    return count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title = @"";
    if (self.pickerType == PickerTypeDate) {
        //
        if (component == 0) {
            title = self.years[row];
        } else if (component == 1) {
            title = self.months[row];
        } else if (component == 2) {
            title = self.days[row];
        }
    } else if (self.pickerType == PickerTypeTime) {
        //
        if (component == 0) {
            title = self.timeHourList[row];
        } else if (component == 1) {
            title = self.timeMinList[row];
        }
    }
    return title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //
//    NSLog(@"%@-%@", @(component).description, @(row).description);
    if (self.pickerType == PickerTypeDate) {
        //
        if (component == 0) {
            self.currentYearStr = self.years[row];
        }else if (component == 1) {
            self.currentMonthStr = self.months[row];
        }else if (component == 2) {
            self.currentDayStr = self.days[row];
        }
        self.days = [self getDayListWithYear:self.currentYearStr month:self.currentMonthStr];
        [self.vpk reloadComponent:2];
    } else if (self.pickerType == PickerTypeTime) {
        //
        if (component == 0) {
            self.currentHour = self.timeHourList[row];
        } else {
            self.currentMin = self.timeMinList[row];
        }
        
    }
}


#pragma mark - setupUI

- (void)setupUI {
    //
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIView *vCover = [[UIView alloc] initWithFrame:keyWindow.frame];
    self.vCover = vCover;
    [keyWindow addSubview:vCover];
    vCover.backgroundColor = [UIColor blackColor];
    vCover.alpha = 0.4;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    [vCover addGestureRecognizer:tap];
    
    //
    self.vpk.showsSelectionIndicator = YES;
    self.backgroundColor = [UIColor whiteColor];
    CGFloat width = [UIScreen mainScreen].bounds.size.width * 4/5;
    //    NSLog(@"width: %@", @(width).description);
    self.bounds = CGRectMake(0, 0, width, width);
    
    self.layer.shadowRadius = 10;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(2, 2);
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    
    //lblHeader
    UILabel *lblHeader = [UILabel gx_labelWithText:@"2016-12-28 周三" fontSize:20 color:kColorBar];
    [self addSubview:lblHeader];
    self.lblHeader = lblHeader;
    
    [lblHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self).mas_offset(0);
        make.left.equalTo(self).mas_offset(10);
        make.height.mas_offset(self.bounds.size.height * 1/5);
    }];
    
    //vLineHeader
    UIView *vLineHeader = [[UIView alloc] init];
    [self addSubview:vLineHeader];
    
    [vLineHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblHeader.mas_bottom).mas_offset(0);
        make.left.right.equalTo(self).mas_offset(0);
        make.height.mas_offset(2);
    }];
    vLineHeader.backgroundColor = kColorBar;
    
    //btnOK
    UIButton *btnOK = [UIButton gx_textButton:@"确认" fontSize:18 normalColor:[UIColor blackColor] selectedColor:nil];
    [self addSubview:btnOK];
    self.btnOK = btnOK;
    
    [btnOK mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self).mas_offset(0);
        make.height.mas_offset(self.bounds.size.height * 1/6);
    }];
    [btnOK addTarget:self action:@selector(btnOKClick:) forControlEvents:UIControlEventTouchUpInside];

    //vLineFooter
    UIView *vLineFooter = [[UIView alloc] init];
    [self addSubview:vLineFooter];
    
    [vLineFooter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self).mas_offset(0);
        make.bottom.equalTo(btnOK.mas_top).mas_offset(0);
        make.height.mas_offset(1);
    }];
    vLineFooter.backgroundColor = [UIColor gx_colorWithRed:200 green:200 blue:200];

    //vPicker
    UIView *vPicker = [[UIView alloc] init];
    [self addSubview:vPicker];
    
    [vPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vLineHeader.mas_bottom).mas_offset(0);
        make.left.right.equalTo(self).mas_offset(0);
        make.bottom.equalTo(vLineFooter.mas_top).mas_offset(0);
    }];

    //-vpk
    UIPickerView *vpk = [[UIPickerView alloc] init];
    [vPicker addSubview:vpk];
    self.vpk = vpk;
    
    [vpk mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(vPicker).mas_offset(0);
    }];
    vpk.delegate = self;
    vpk.dataSource = self;
    

}


- (void)initLinesWithCount:(NSInteger)count {
    //lines
    NSMutableArray<UIView *> *linesM = [NSMutableArray<UIView *> array];
    for (int i = 0; i < count; i++) {
        UIView *vLine = [[UIView alloc] init];
        [self addSubview:vLine];
        vLine.backgroundColor = kColorBar;
        [linesM addObject:vLine];
    }
    self.linesM = linesM;
    
    //layout
    if (linesM.count == 4) {
        //
        CGFloat centerxF = self.bounds.size.width * 9/40;
        [linesM[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(-centerxF);
            make.centerY.equalTo(self).mas_offset(-18); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[1] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(-centerxF);
            make.centerY.equalTo(self).mas_offset(28);
            make.size.mas_equalTo(linesM[0]);
        }];
        
        [linesM[2] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(centerxF);
            make.centerY.equalTo(self).mas_offset(-18);
            make.size.mas_equalTo(linesM[0]);
        }];
        
        [linesM[3] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(centerxF);
            make.centerY.equalTo(self).mas_offset(28);
            make.size.mas_equalTo(linesM[0]);
        }];
        
        //:
        UILabel *lblSepa = [UILabel gx_labelWithText:@":" fontSize:30 color:[UIColor grayColor]];
        [self addSubview:lblSepa];
        
        [lblSepa mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(0);
            make.centerY.equalTo(self).mas_offset(2);
        }];

    } else if (linesM.count == 6) {
        //
        CGFloat centerxF = self.bounds.size.width * 13/40;
        [linesM[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(-centerxF);
            make.centerY.equalTo(self).mas_offset(-18); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[1] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(-centerxF);
            make.centerY.equalTo(self).mas_offset(28); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[2] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(0);
            make.centerY.equalTo(self).mas_offset(-18); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[3] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(0);
            make.centerY.equalTo(self).mas_offset(28); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[4] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(centerxF);
            make.centerY.equalTo(self).mas_offset(-18); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        [linesM[5] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).mas_offset(centerxF);
            make.centerY.equalTo(self).mas_offset(28); //可以固定值
            make.size.mas_offset(CGSizeMake(self.bounds.size.width * 1/4, 2));
        }];
        
        
    }
}



#pragma mark - setter & getter

- (void)setTitle:(NSString *)title {
    _title = title.copy;
    self.lblHeader.text = _title;
}

- (void)setPickerType:(PickerType)pickerType {
    _pickerType = pickerType;
    switch (pickerType) {
        case PickerTypeDate:
            [self initForDate];
            break;
        case PickerTypeTime:
            [self initForTime];
            break;
            
        default:
            break;
    }
}


- (NSArray<NSString *> *)years {
    if (!_years) {
        _years = [NSArray<NSString *> array];
    }
    return _years;
}

- (NSArray<NSString *> *)months {
    if (!_months) {
        _months = [NSArray<NSString *> array];
    }
    return _months;
}

- (NSArray<NSString *> *)days {
    if (!_days) {
        _days = [NSArray<NSString *> array];
    }
    return _days;
}

- (NSArray<NSString *> *)timeHourList {
    if (!_timeHourList) {
        _timeHourList = [NSArray<NSString *> array];
    }
    return _timeHourList;
}

- (NSArray<NSString *> *)timeMinList {
    if (!_timeMinList) {
        _timeMinList = [NSArray<NSString *> array];
    }
    return _timeMinList;
}




@end
